context("testing Partial Signature Extraction, this should not take more than 10 minutes")

test_that("test that SignatureExtraction() works on a random matrix and using partial extraction", {
  
  #switch off warnings
  options(warn=-1)
  
  #test rearrangements
  n_row <- 32
  n_col <- 15
  rnd_matrix <- round(matrix(runif(n_row*n_col,min = 0,max = 50),nrow = n_row,ncol = n_col))
  colnames(rnd_matrix) <- paste0("C",1:n_col)
  row.names(rnd_matrix) <- paste0("R",1:n_row)
  matrix_of_fixed_signatures <- matrix(runif(n_row*1,min = 0,max = 50),ncol=1,dimnames = list(row.names(rnd_matrix),c("known1")))
  matrix_of_fixed_signatures <-  matrix_of_fixed_signatures/matrix(data=rep(apply(matrix_of_fixed_signatures,2,sum),nrow(matrix_of_fixed_signatures)),nrow = nrow(matrix_of_fixed_signatures),byrow = TRUE)
  suppressWarnings(SignatureExtraction(cat = rnd_matrix,
                      outFilePath = paste0("extraction_test_rearr/"),
                      matrix_of_fixed_signatures = matrix_of_fixed_signatures,
                      nrepeats = 10,
                      nboots = 2,
                      nparallel = 2,
                      nsig = 1:3,
                      mut_thr = 0,
                      type_of_extraction = "rearr",
                      project = "test",
                      parallel = TRUE,
                      nmfmethod = "brunet"))
  
  #here goes the test
  expect_true(file.exists("extraction_test_rearr/Sigs_OverallMetrics_test_nboots2.jpg"))
  
  #test substitutions
  mut.order <- c("A[C>A]A","A[C>A]C","A[C>A]G","A[C>A]T","C[C>A]A","C[C>A]C","C[C>A]G","C[C>A]T","G[C>A]A","G[C>A]C","G[C>A]G","G[C>A]T","T[C>A]A","T[C>A]C","T[C>A]G","T[C>A]T","A[C>G]A","A[C>G]C","A[C>G]G","A[C>G]T","C[C>G]A","C[C>G]C","C[C>G]G","C[C>G]T","G[C>G]A","G[C>G]C","G[C>G]G","G[C>G]T","T[C>G]A","T[C>G]C","T[C>G]G","T[C>G]T","A[C>T]A","A[C>T]C","A[C>T]G","A[C>T]T","C[C>T]A","C[C>T]C","C[C>T]G","C[C>T]T","G[C>T]A","G[C>T]C","G[C>T]G","G[C>T]T","T[C>T]A","T[C>T]C","T[C>T]G","T[C>T]T","A[T>A]A","A[T>A]C","A[T>A]G","A[T>A]T","C[T>A]A","C[T>A]C","C[T>A]G","C[T>A]T","G[T>A]A","G[T>A]C","G[T>A]G","G[T>A]T","T[T>A]A","T[T>A]C","T[T>A]G","T[T>A]T","A[T>C]A","A[T>C]C","A[T>C]G","A[T>C]T","C[T>C]A","C[T>C]C","C[T>C]G","C[T>C]T","G[T>C]A","G[T>C]C","G[T>C]G","G[T>C]T","T[T>C]A","T[T>C]C","T[T>C]G","T[T>C]T","A[T>G]A","A[T>G]C","A[T>G]G","A[T>G]T","C[T>G]A","C[T>G]C","C[T>G]G","C[T>G]T","G[T>G]A","G[T>G]C","G[T>G]G","G[T>G]T","T[T>G]A","T[T>G]C","T[T>G]G","T[T>G]T")
  n_row <- 96
  n_col <- 15
  rnd_matrix <- round(matrix(runif(n_row*n_col,min = 0,max = 50),nrow = n_row,ncol = n_col))
  colnames(rnd_matrix) <- paste0("C",1:n_col)
  row.names(rnd_matrix) <- mut.order
  matrix_of_fixed_signatures <- matrix(runif(n_row*2,min = 0,max = 50),ncol = 2,dimnames = list(row.names(rnd_matrix),c("known1","known2")))
  matrix_of_fixed_signatures <-  matrix_of_fixed_signatures/matrix(data=rep(apply(matrix_of_fixed_signatures,2,sum),nrow(matrix_of_fixed_signatures)),nrow = nrow(matrix_of_fixed_signatures),byrow = TRUE)
  suppressWarnings(SignatureExtraction(cat = rnd_matrix,
                      outFilePath = paste0("extraction_test_subs/"),
                      matrix_of_fixed_signatures = matrix_of_fixed_signatures,
                      nrepeats = 10,
                      nboots = 2,
                      nparallel = 2,
                      nsig = 1:3,
                      mut_thr = 0,
                      type_of_extraction = "subs",
                      project = "test",
                      parallel = TRUE,
                      nmfmethod = "brunet"))
  
  #here goes the test
  expect_true(file.exists("extraction_test_subs/Sigs_OverallMetrics_test_nboots2.jpg"))
  
  #test generic
  n_row <- 10
  n_col <- 15
  rnd_matrix <- round(matrix(runif(n_row*n_col,min = 0,max = 50),nrow = n_row,ncol = n_col))
  colnames(rnd_matrix) <- paste0("C",1:n_col)
  row.names(rnd_matrix) <- paste0("R",1:n_row)
  matrix_of_fixed_signatures <- matrix(runif(n_row*2,min = 0,max = 50),ncol=2,dimnames = list(row.names(rnd_matrix),c("known1","known2")))
  matrix_of_fixed_signatures <-  matrix_of_fixed_signatures/matrix(data=rep(apply(matrix_of_fixed_signatures,2,sum),nrow(matrix_of_fixed_signatures)),nrow = nrow(matrix_of_fixed_signatures),byrow = TRUE)
  suppressWarnings(SignatureExtraction(cat = rnd_matrix,
                      outFilePath = paste0("extraction_test_generic/"),
                      matrix_of_fixed_signatures = matrix_of_fixed_signatures,
                      nrepeats = 10,
                      nboots = 2,
                      nparallel = 2,
                      nsig = 1:3,
                      mut_thr = 0,
                      type_of_extraction = "generic",
                      project = "test",
                      parallel = TRUE,
                      nmfmethod = "brunet"))
  
  #here goes the test
  expect_true(file.exists("extraction_test_generic/Sigs_OverallMetrics_test_nboots2.jpg"))  
  
  #remove the test directory
  unlink("extraction_test_rearr/", recursive = TRUE)
  unlink("extraction_test_subs/", recursive = TRUE)
  unlink("extraction_test_generic/", recursive = TRUE)
  
  #switch warnings back on
  options(warn=0)
  
})